package com.grsu.TransportSystem.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.grsu.TransportSystem.dao.IPersonDAO;
import com.grsu.TransportSystem.domain.Person;

@Repository
public class PersonDaoImpl implements IPersonDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void create(Person object)  {
		// TODO Auto-generated method stub

	}

	@Override
	public List<Person> read(Person object)  {
		List<Person> list = null;
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Person.class);
		if(object.getId() != null)
			criteria.add(Restrictions.eq("id", object.getId()));
		if(object.getName() != null)
			criteria.add(Restrictions.eq("name", object.getName()));
		if(object.getLogin() != null)
			criteria.add(Restrictions.eq("login", object.getLogin()));
		if(object.getPassword() != null)
			criteria.add(Restrictions.eq("password", object.getPassword()));
		if(object.getRole() != null)
			criteria.add(Restrictions.eq("post", object.getRole()));
		if(object.getSurname() != null)
			criteria.add(Restrictions.eq("surname", object.getSurname()));
		list = (ArrayList<Person>) criteria.list();
		return list;
	}

	@Override
	public void update(Person object)  {
		// TODO Auto-generated method stub

	}

	@Override
	public void delete(Person object)  {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	@Override
	public Person findByLoginandPassword(String login,String password){
		List<Person> list;
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Person.class);
		criteria.add(Restrictions.eq("login", login));
		criteria.add(Restrictions.eq("password", password));
		list = (ArrayList<Person>) criteria.list();
		Person temp = null;
		if(list.size()>0)
			temp = list.get(0);

		return temp;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Person findByLogin(String login){
		List<Person> list;
		Session session = sessionFactory.getCurrentSession();
		Criteria criteria = session.createCriteria(Person.class);
		criteria.add(Restrictions.eq("login", login));
		list = (ArrayList<Person>) criteria.list();
		Person temp = null;
		if(list.size()>0)
			temp = list.get(0);

		return temp;
	}

	@Override
	public List<Person> findByRole(String role) {
		Session session = sessionFactory.getCurrentSession();
		if(role != null)
		{
		Query q = session.createQuery("from Person where post =  :role");
		q.setParameter("role", role);
		return  (List<Person>) q.list();
		}
		else
		{throw new RuntimeException();}
	}

}
